<!-- 管理员增加课程，肯定也需要课程的id -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加课程</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
    <el-form-item label="课程号" prop="courseId">
          <el-input v-model="ruleForm.courseId"></el-input>
        </el-form-item>
        <el-form-item label="学院号" prop="collegeId">
          <el-input v-model="ruleForm.collegeId"></el-input>
        </el-form-item>
        <el-form-item label="学分" prop="credit">
          <el-input v-model.number="ruleForm.credit"></el-input>
        </el-form-item>
        <el-form-item label="课程名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="学时" prop="period">
          <el-input  v-model="ruleForm.period"></el-input>
        </el-form-item>
        <el-form-item label="学期" prop="term">
          <el-input  v-model="ruleForm.term"></el-input>
        </el-form-item>
        <el-form-item label="上课时间" prop="timeSchedule">
          <el-input  v-model="ruleForm.timeSchedule"></el-input>
        </el-form-item>
        <el-form-item label="上课地点" prop="location">
          <el-input  v-model="ruleForm.location"></el-input>
        </el-form-item>
      
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
     
    </el-form>
  </div>
</template>

<script>
export default {
  name: "InsertCourseInfo",
  data() {
    return {
      
      title: "添加课程",
      ruleForm: {courseId:'',name:'',collegeId:'',credit:'',term:'',period:'',timeSchedule:'',location:''},
      rules: {
        courseId: [{ required: true, message: "请填写课程ID" }],
        courseName: [{ required: true, message: "请填写课程名称" }],
        collegeId: [{ required: true, message: "请填写学院ID" },
        {
          validator: (rule, value, callback) => {
  const numericValue = parseInt(value, 10); // 将值转换为整数
  if (!Number.isInteger(numericValue)) {
    callback(new Error("学期必须为整数"));
  } else {
    callback();
  }}}
      ],

        credit: [{ required: true, message: "请填写学分" }],
        term: [{ required: true, message: "请填写学期" },
        {
          validator: (rule, value, callback) => {
  const numericValue = parseInt(value, 10); // 将值转换为整数
  if (!Number.isInteger(numericValue)) {
    callback(new Error("学期必须为整数"));
  } else {
    callback();
  }
}

    }
      ],
        period: [{ required: true, message: "请填写学时" }],
        timeSchedule: [{ required: true, message: "请填写时间安排" },
        {
    pattern: /^(monday|tuesday|wednesday|thursday|friday|saturday|sunday),([1-9]|10|11)-([1-9]|10|11)$/,
    message: '请输入正确的时间安排，例如：monday,1-3'
  },
  {
    validator: (rule, value, callback) => {
      const [, range] = value.split(',');
      const [start, end] = range.split('-').map(Number);

      if (start < end) {
        callback(); // 符合规则
      } else if(end>11){
        callback(new Error('上课节数不能超过11节'));
      } else {
        callback(new Error('前面的数字应大于后面的数字'));
      }
    }
  }
],
        location: [{ required: true, message: "请填写地点安排" }]
      },
    };
  
  },
  created(){
    if(localStorage.getItem("type") !== 'admin'){this.$router.push(`/login`);this.$message.error('权限不够');}
  },
  methods: {
   
      //添加课程信息
      async insertcourseInfo() {
      // JSON化
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'token':`${localStorage.getItem("token")}`
        }
      };
      try {
        const { data: res } = await this.$http.post(`api/admin/course/insert`,JSON.stringify(this.ruleForm),config);
        if (res.status !== 500) {
          this.$message.success('成功添加课程信息');
          // 重新调用获取表格的代码
        } else {
          this.$message.error('添加失败');
        }
      } catch (error) {
        // Handle request error
        console.error('Request Error:', error);
        this.$message.error('添加失败');
    }
      },
      //提交表单信息
      submitForm(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            this.insertcourseInfo();
          }
          else{
            this.$message.error('请将信息填写完整');
          }
        });
      },
      // 重置清空表单信息
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  
   
  }

</script>

<style lang="less" scoped>
.el-breadcrumb {
  padding-bottom: 10px;
}
.el-input {
  width: 40%;
}
/deep/ .el-textarea__inner {
  width: 40%;
  line-height: 3;
}
.el-form-item {
  margin: 18px 0 !important;
}
</style>
